package view;

import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.JFrame;
import javax.swing.JPanel;

import controller.GameSettings;
import controller.GameState;
import controller.Manager2Gui;
import controller.human.Action;
import controller.human.Person;
import controller.human.User;
import java.awt.Toolkit;

import view.pane.DayPane;
import view.pane.NightPaneResult;
import view.pane.RootPane;
import view.pane.VotingResultsPane;

public class Gui implements Gui2Manager {
	public JFrame window;
	String adminName = "";
	String adminPass = "";
	GameSettings settings;
	Manager2Gui manager;
	Set<User> users = new TreeSet<User>();
	
    @Override
	public void init(Manager2Gui manager) {
		this.manager = manager;
		window = new JFrame("Mafia StoryTeller");
                window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
                window.setResizable(false);
		window.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		
		updatePane(new RootPane(this));
	}

	public void updatePane(JPanel newPanel) {
		window.getContentPane().removeAll();
		window.getContentPane().invalidate();

		window.getContentPane().add(newPanel);
		window.getContentPane().validate();
		window.setVisible(true);
	}
	
	public void setAdmin(String adminName, String adminPass) {
		this.adminName = adminName;
		this.adminPass = adminPass;
	}
	
	public void setSettings(GameSettings settings) {
		this.settings = settings;
		
	}

	public Set<User> getUsers() {
		return users;
	}

	public void addUser(User user) {
		users.add(user);
	}

	public void removeUser(User user) {
		users.remove(user);
	}

	public void changeState(GameState newState) {
		manager.changeState(newState);
	}

	public void nightAction(User actor, User target, Action action) {
		manager.nightAction(actor, target, action);
	}
	
	public void nightResult(User target, Boolean isKiller) {
		// user already did an action this night
		if (isKiller == null) {
			updatePane(new NightPaneResult(this));
		} else {
			updatePane(new NightPaneResult(this, target, isKiller));			
		}
	}

	
	public void dayResult(User killed, User handicapped, Action handicappAction) {
		manager.changeState(GameState.Debate);
		updatePane(new DayPane(this, killed, handicapped, handicappAction));
	}

	public boolean existProposal() {
		return manager.existProposal();
	}

	public void addProposal(User user, User proposed) {
		manager.addProposal(user, proposed);
		
	}

	public void addVote(User actor, User target) {
		manager.addVote(actor, target);
	}

	public void voteResults(Map<User, User> votes, User killed) {
		updatePane(new VotingResultsPane(this, votes, killed));
	}

	public void startNewSession() {
		manager.startNewSession();
	}

    @Override
    public void nightResult(User target, boolean killer) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
